<?
    class Principio_Model_SpaExtTemplatesMapper extends Principio_Library_Mapper
    {
        private static $db_table = null;

        private static function getDBTable()
        {
            if (self::$db_table == null)
                self::$db_table = new Principio_Model_DbTable_SpaExtTemplates();

            return self::$db_table;
        }

        public static function delete($spa_id, $use_transaction = false)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                self::getDBTable()->delete(array("spa_id=?" => intval($spa_id)));

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                $return = self::reportException($ex);

                if ($use_transaction)
                {
                    self::getDBTable()->getAdapter()->rollback();
                    return false;
                }
                else
                    return $return;

            };
        }

        public static function update(Principio_Model_SpaExtTemplates $spa_ext_templates)
        {
            try
            {
                for ($i = 0; $i < $spa_ext_templates->getExts(); $i++)
                    self::getDBTable()->update(array("template" => $spa_ext_templates->getTemplate($i)), array("spa_id=?" => $spa_ext_templates->getSpaID(), "ext_number=?" => $i));

            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function insert(Principio_Model_SpaExtTemplates $spa_ext_templates)
        {
            try
            {
                for ($i = 0; $i < $spa_ext_templates->getExts(); $i++)
                    self::getDBTable()->insert(array("template" => $spa_ext_templates->getTemplate($i), "spa_id" => $spa_ext_templates->getSpaID(), "ext_number" => $i));

            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function get($spa_id)
        {
            try
            {
                $result = self::getDBTable()->fetchAll(self::getDBTable()->select()->where("spa_id=?", $spa_id));
                return new Principio_Model_SpaExtTemplates($result->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

        public static function search($string)
        {
            $search_fields = array(
                "template" => "Template"
            );
            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);
            $search = array();
            foreach ($result as $res)
            {
                $spa_info = Principio_Model_SpaMapper::get($res["spa_id"]);
                //echo "<pre>"; print_r($spa_info);
                $model_search = new Principio_Model_Search($string, "Spa");
                $model_search->setID($res["spa_id"])->setResultName($spa_info->getIP()." [".$spa_info->getMAC()."]")->setOpenItemFunctionName("Spa")->setParentID(false);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["id"]] = $model_search;
            }
            return $search;
        }
    }
?>